package com.nm.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.nm.commom.Result;
import com.nm.mapper.*;
import com.nm.pojo.*;
import com.nm.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import static java.lang.System.out;
/**
 *消费者接口
 */
//@CrossOrigin
@RestController
@RequestMapping(value = "/c",produces="application/json;charset=utf-8")
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;
    @Autowired
    private ConsumerMapper consumerMapper;

    @Autowired
    private StallMapper stallMapper;

    @Autowired
    private StopMapper stopMapper;

    @Autowired
    private ExpendMapper expendMapper;

    @Autowired
    private IncomeMapper incomeMapper;

    @Autowired
    private TransactionMapper transactionMapper;


    /**
     *微信授权登录
     * @apiNote 除nickName外的信息都可以不填 id和loginTime不用填
     */
    //
    @RequestMapping(value = "/login")
    public Result addConsumer(@RequestBody  Consumer consumer){
        QueryWrapper<Consumer> consumerQueryWrapper = new QueryWrapper<>();
        consumerQueryWrapper.eq("nick_name", consumer.getNickName());
        Consumer consumer1 = consumerMapper.selectOne(consumerQueryWrapper);
        if(consumer1 == null) {
            Income income = new Income();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            consumer.setLoginTime(simpleDateFormat.format(new Date()));
            consumerService.addConsumer(consumer);
            String nickName = consumer.getNickName();
            income.setNickName(nickName);
            int i = consumerService.addIncome(income);//创建收入者钱包
        }
        return Result.success(consumer1);
    }

    /**
     *查询全部用户信息
     */
    //
    @RequestMapping(value = "/queryAll")
    public Result queryallConsumer(){
        List<Consumer> list = consumerMapper.selectList(null);
//        String json = JSON.toJSONString(list);
        out.println(list);
        return Result.success("200",list);
    }

    /**
     *根据ID查询用户信息
     */
    //
    @GetMapping(value = "/queryId")
    public Result queryId(@RequestBody Daom daom){
        int userId = daom.getId();
        Consumer consumer = consumerMapper.selectById(userId);
        return Result.success(consumer);
    }

    /**
     *根据用户名查询用户信息
     */
    //
    @GetMapping(value = "/queryName")
    public Result queryId(@RequestParam String nickName){
        QueryWrapper<Consumer> consumerQueryWrapper = new QueryWrapper<>();
        consumerQueryWrapper.eq("nick_name",nickName);
        Consumer consumer = consumerMapper.selectOne(consumerQueryWrapper);
        return Result.success(consumer);
    }

    /**
     *修改用户信息 要传id
     * @apiNote 钱包、登陆时间不能改
     */
    //
    @PostMapping(value = "/update")
    public Result updateConsumer(@RequestBody Consumer consumer){
        int i = consumerMapper.updateById(consumer);
        return Result.success(1);
    }

    /**
     *删除用户信息 传id
     */
    //
    @PostMapping(value = "/delete")
    public Result deleteConsumer(@RequestBody Daom daom){
        int userid = daom.getId();
        int i = consumerMapper.deleteById(userid);
        return Result.success(i);
    }

    /**
     *查询钱包
     * @param nickname 用户名
     */
    //
    @RequestMapping(value = "/queryMoney")
    public Result querymoneyConsumer(@RequestParam  String nickname){
        out.println(nickname);
        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        wrapper.eq("nick_name",nickname);
        List<Consumer> list = consumerMapper.selectList(wrapper);
        Consumer consumer = list.get(0);
        int money = consumer.getUserMoney();
        return Result.success(money);
    }

    /**
     *钱包充值
     * @param nickName 用户名
     * @param money 充值金额
     */
    //
    @RequestMapping(value = "/addMoney")
    public Result addmoneyConsumer(@RequestParam (value = "nickName")String nickName,
                                   @RequestParam(value = "money") int money){
        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        wrapper.eq("nick_name",nickName);
        out.println(money);
        Consumer con = consumerMapper.selectOne(wrapper);
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("nick_name", nickName);
        updateWrapper.set("user_money", con.getUserMoney()+money);
        consumerMapper.update(null, updateWrapper);
        Transaction transaction = new Transaction();
        transaction.setMoney(money);
        Date date = new Date();
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = simple.format(date);
        transaction.setTime(nowTime);
        transaction.setNickName(nickName);
        transactionMapper.insert(transaction);
        return Result.success(con.getUserMoney()+money);
    }

    /**
     *查询全部充值记录
     */
    @RequestMapping(value = "/queryT")
    public Result queryT(){
        List<Transaction> transactions = transactionMapper.selectList(null);
        return Result.success(transactions);
    }

    /**
     *根据用户名查询充值记录
     */
    @RequestMapping(value = "/queryTname")
    public Result queryT(@RequestParam String nickName){
        QueryWrapper<Transaction> transactionQueryWrapper = new QueryWrapper<>();
        transactionQueryWrapper.eq("nick_name",nickName);
        List<Transaction> transactions = transactionMapper.selectList(transactionQueryWrapper);
        return Result.success(transactions);
    }

    /**
     *钱包提现
     * @param nickName 用户名
     * @param userMoney 提现金额
     */
    //
    @RequestMapping(value = "/deleteMoney")
    public Result deletemoneyConsumer(@RequestParam String nickName,@RequestParam int userMoney){
        String jg;
        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        wrapper.eq("nick_name",nickName);
        List<Consumer> list = consumerMapper.selectList(wrapper);
        Consumer con = list.get(0);
        int money = con.getUserMoney();
        if (money<userMoney){
            return Result.error(401,"余额不足");
        }else {
            money = money - userMoney;
            Consumer consumer = new Consumer();
            consumer.setUserMoney(money);
            consumerMapper.update(consumer, wrapper);
            return Result.success(consumer.getUserMoney());
        }
    }

    /**
     *停车租用结束、评分 传stopId、useStatus 评分的话还要传scoreStatus(1)、score
     */
    //
    @RequestMapping(value = "/deduct")
    public Result deductMoney(@RequestBody Stop stop) throws ParseException {
        stopMapper.updateById(stop);
        Stop stop1 = stopMapper.selectById(stop);
        Stall stall = stallMapper.selectById(stop1.getStallId());
        stall.setStallState("出租");
        stallMapper.updateById(stall);
        return Result.success();
        /*Stop stop = stopMapper.selectById(stopId);
        Date date = new Date();
        SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime = simple.format(date);
        stop.setOverTime(nowTime);
        Long i = simple.parse(nowTime).getTime() - simple.parse(stop.getStartTime()).getTime();
        Long hour  = i / 3600000;
        QueryWrapper<Stall> stallQueryWrapper = new QueryWrapper<>();
        stallQueryWrapper.eq("stall_id",stop.getStallId());
        Stall stall1 = stallMapper.selectOne(stallQueryWrapper);
        long price = hour * stall1.getPrice();
        int useMoney=(int) (hour*price);

        Expend expend = new Expend();

        Stall stall = new Stall();

        Consumer consumer = new Consumer();
        QueryWrapper<Consumer> wrapper = new QueryWrapper<>();
        wrapper.eq("nick_name",stop.getNickName());
        Consumer consumer1 = consumerMapper.selectOne(wrapper);
        int money = consumer1.getUserMoney();
        if (money < useMoney){
            return Result.error(401,"余额不足");
        }else {
            int money1 = money - useMoney;
            consumer.setUserMoney(money1);
            consumerMapper.update(consumer,null);
            stopMapper.updateById(stop); //添加停车记录
            double userMoney = useMoney;
            double ptMoney = money - useMoney;
            QueryWrapper<Income> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("nick_name",stop.getNickName());
            List<Income> list1 = incomeMapper.selectList(wrapper1);
            Income income = list1.get(0);
            double incomeMoney = income.getIncomeMoney();
            incomeMoney = incomeMoney +userMoney;
            income.setIncomeMoney(incomeMoney);
            incomeMapper.update(income,wrapper1); //80%添加到出租者
            expend.setTotalMoney(money);
            expend.setUserMoney(userMoney);
            expend.setPtMoney(ptMoney);
            expend.setExpendTime(nowTime);
            expend.setNickName(stop.getNickName());
            expendMapper.insert(expend); //添加支出记录
            return Result.success("支付成功");
        }*/
    }

    /**
     *查询钱包收入
     */
    //
    @RequestMapping(value = "/queryIncome")
    public Result queryIncome(String nickName){
        QueryWrapper<Income> wrapper = new QueryWrapper<>();
        wrapper.eq("nick_name",nickName);
        List<Income> list = incomeMapper.selectList(wrapper);
        return Result.success(list);
    }

    /**
     *钱包收入提现
     */
    //
    @RequestMapping(value = "/txIncome")
    public Result txIncome(String nickName,int txMoney){
        String jg = null;
        Income income = new Income();
        QueryWrapper<Income> wrapper = new QueryWrapper<>();
        wrapper.eq("nick_name",nickName);
        QueryWrapper<Consumer> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("nick_name",nickName);
        List<Income> list = incomeMapper.selectList(wrapper);
        Income income1 = list.get(0);
        double incomeMoney = income1.getIncomeMoney();
        if (incomeMoney < txMoney){
            return Result.error(401,"余额不足");
        }else {
            incomeMoney = incomeMoney - txMoney;
            income.setIncomeMoney(incomeMoney);
            incomeMapper.update(income,wrapper);

            List<Consumer> list1 = consumerMapper.selectList(wrapper1);
            Consumer consumer = list1.get(0);
            int userMoney = consumer.getUserMoney();
            userMoney = userMoney +txMoney;
            consumer.setUserMoney(userMoney);
            consumerMapper.update(consumer,wrapper1);

            return Result.success("提现成功");
        }

    }

}
